package org.idea.spring.framework.tx.config.druid;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @Author linhao
 * @Date created in 12:20 下午 2021/5/17
 */
@Slf4j
@Configuration
public class DruidDataSourceConfig {

    @Bean
    @ConditionalOnExpression("#{'druid'.equals(environment['qiyu.default.datasource.type'])}")
    public DruidSimpleDataSourceProperties druidSimpleDataSourceProperties(){
        return new DruidSimpleDataSourceProperties();
    }

    @Bean
    @ConditionalOnBean(value = DruidSimpleDataSourceProperties.class)
    public DruidDataSource druidDataSource(DruidSimpleDataSourceProperties druidSimpleDataSourceProperties){
        log.info("[DruidDataSourceConfig] druidDataSource is : "+druidSimpleDataSourceProperties.toString());
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(druidSimpleDataSourceProperties.getUrl());
        druidDataSource.setUsername(druidSimpleDataSourceProperties.getUsername());
        druidDataSource.setPassword(druidSimpleDataSourceProperties.getPassword());
        druidDataSource.setDriverClassName(druidSimpleDataSourceProperties.getDriver());
        return druidDataSource;
    }
}
